package com.iptv.grape.domain;

import com.iptv.grape.BaseActivity;
import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.net.Entity.ChannelBillData;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.video.XMediaPlayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.iptv.grape.bu.GrapeContext.curGrapeContext;

// 频道节目单
public class ChannelBill implements Comparable<ChannelBill>, IListViewShowItem {
    // 节目单ID
    private String keyID;
    // 节目单编码
    private String code;
    // 节目单名称
    private String name;
    // 开始时间
    private String startDate;
    // 结束日期
    private String endDate;
    // 时间长度
    private int duration;
    // 海报
    private String posters;
    // 详细介绍
    private String desc;
    // 回看参数
    private String args;
    // 频道ID
    private String channelID;

    // 日期对象 节目单开始时间
    private Date billStartDate;

    // 日期对象 节目单结束时间
    private Date billEndDate;

    // 是否已经预约
    private boolean isAppointment = false;

    public boolean isAppointment() {
        return isAppointment;
    }



    public void setAppointment(boolean appointment) {
        isAppointment = appointment;
    }

    public Date getBillStartDate() {
        return billStartDate;
    }

    public Date getNewBillStartDate() {
        Date date = new Date(billStartDate.getTime());
        return date;
    }

    public Date getBillEndDate() {
        return billEndDate;
    }

    public ChannelBill(String keyID, String code, String name, String startDate,
                       String endDate, int duration, String posters, String desc, String args, String channelID) {
        this.keyID = keyID;
        this.code = code;
        this.name = name;
        this.startDate = startDate;
        this.endDate = endDate;
        this.duration = duration;
        this.posters = posters;
        this.desc = desc;
        this.args = args;
        this.channelID = channelID;
        this.billStartDate = CommUtil.toDate(this.startDate);
        this.billEndDate = CommUtil.toDate(this.endDate);

    }

    public ChannelBill(ChannelBillData billData) {
        this(billData.getKeyID(), billData.getCode(), billData.getName(), billData.getStartDate(), billData.getEndDate(),
                billData.getDuration(), billData.getPosters(), billData.getDesc(), billData.getArgs(), billData.getChannelID());
    }

    public String getKeyID() {
        return keyID;
    }

    public String getCode() {
        return code;
    }

    @Override
    public int getSort() {
        return 0;
    }

    @Override
    public String getText() {
        return this.getName();
    }

    @Override
    public List<IListViewShowItem> getChList() {

        return new ArrayList<IListViewShowItem>();
    }
    // 进入回看节目模式
    @Override
    public void itemClick(List<IListViewShowItem> dataSouce, int pos, ListViewUI listViewUI) {
        BaseActivity activity = curGrapeContext().getCurActivity();
        if(activity != null){
            String curType = this.getBillType();
            Channel channel = GrapeContext.curGrapeContext().findChannelByID(this.channelID);
            if(channel != null) {
                if (curType.equals("now")) {
                    // 直播
                    channel.playChannel();
                }
                if (curType.equals("before")) {
                    if (AppConfig.getAppConfig().isCanBackLive()) {
                        // 进入回看
                        curGrapeContext().hiddLiveMenu(activity);
                        curGrapeContext().loadChannelBillUI(activity, CommUtil.changeTime(this.billStartDate), this, pos, dataSouce);
                        curGrapeContext().getCurActivity().statuVisible(channel.getName(), "回看");
                        channel.playChannelBill(this);
                    }
                }
                if(curType.equals("future")){
                    // 预约
                    //isAppointment = true;
                    activity.showChannelBill(channel,this,isAppointment,listViewUI);
                    isAppointment = true;
                }
            }
        }

    }

    @Override
    public ListViewStyle getListViewStyle() {

        if(AppConstant.isTvStb()) {
            return new ListViewStyle(214, "channelBillListItem", "ChannelBill",
                    5, 2, 0, 86, "#c0c0c0");
        }
        else {
            return new ListViewStyle(214, "channelBillListItem", "ChannelBill",
                    7, 3, 0, 86, "#c0c0c0");
        }



    }

    public String getName() {
        return name;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public int getDuration() {
        return duration;
    }

    public String getPosters() {
        return posters;
    }

    public String getDesc() {
        return desc;
    }

    public String getArgs() {
        return args;
    }

    public String getChannelID() {
        return channelID;
    }

    @Override
    public int compareTo(ChannelBill channelBill) {
        return this.billStartDate.compareTo(channelBill.billStartDate);
    }

    // 获得节目单上显示的时间 开始时间 - 结束时间
    public String getShowTimeText() {
        return this.startDate.substring(this.startDate.length() - 8, this.startDate.length() - 3) + "-" + this.endDate.substring(this.endDate.length() - 8, this.endDate.length() - 3);
    }

    // 获得节目单的类别
    public String getBillType() {

        long nowTime = System.currentTimeMillis();
        if (nowTime <= this.billEndDate.getTime() && nowTime >= this.billStartDate.getTime()) {
            return "now";       // 现在
        } else if (nowTime > this.billEndDate.getTime()) {
            return "before";    // 以前
        } else {
            return "future";    // 未来
        }
    }

//Date data1 = CommUtil.toDate("2019-08-25 17:40:00");
    //Date data2 = CommUtil.toDate("2019-08-25 02:27:00");


    //int mm = data1.compareTo(data2);
}
